package cn.gwm.flink.streaming.strategy.vehiclepredict;

import cn.gwm.flink.streaming.constant.ChargePredictFields;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.state.MapState;

import java.io.Serializable;

/**
 * @author : GW00280745
 * @date : 2022/5/14 11:05
 * @description :首条记录 离充电起始点最近1分钟的记录为首条记录
 */
public class FirstRecord implements Serializable {

    public void process(JSONObject record,
                        MapState<String, String> frState){
        try {
            fr(record,frState);
            fr2Record(record,frState);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * @description: 更新fr状态
     *      tid与初始点tid的diff大于1min，并且状态中fr是空 则 更新状态   frState中的值格式(vin,diff#tid)
     * @author:  GW00280745
     * @date: 2022/8/15 8:10
     * @param:
     * @return:
     **/
    private void fr(JSONObject record,MapState<String, String> frState) throws  Exception{

        String vin = record.getString(ChargePredictFields.ins().srcFieldVin);
        //fr的状态是否为空
        String chargeGroupName = record.getString(ChargePredictFields.ins().dwsFieldChargeBegin);
        String frFlag = frState.get(chargeGroupName);
        if ( StringUtils.isNotBlank(frFlag)){
            return;
        }
        //frb的tid
        String frb = record.getString(ChargePredictFields.ins().dwsFieldFrbField);
        String frbTid = StringUtils.substringAfterLast(frb, ChargePredictFields.ins().char3);

        //record的tid
        String tid = record.getString(ChargePredictFields.ins().srcFieldTid);
        //如果diff>=1min那么更新状态
        try {
            long frbTidL = Long.parseLong(frbTid);
            boolean b = Long.parseLong(tid) - frbTidL >= 60 * 1000;
            if (!b){
                return;
            }
        }catch (NumberFormatException nfe){
            return;
        }
        String frFieldVal = vin.concat(ChargePredictFields.ins().char3).concat(tid);
        frState.put(chargeGroupName,frFieldVal);
        record.put(ChargePredictFields.ins().stdFieldChargeInitPoint,frFieldVal);
        record.put(ChargePredictFields.ins().stdFieldChargeInitPointFlag,ChargePredictFields.ins().oneStr);
    }
    /**
     * @description: 将frState中的值进行赋值并输出，如果tid大于等于frTid那么进行复制否则就是空
     * @author:  GW00280745
     * @date: 2022/8/15 8:10
     * @param:
     * @return:
     **/
    private void fr2Record(JSONObject record,MapState<String, String> frState) throws Exception{
        String frFlag = record.getString(ChargePredictFields.ins().stdFieldChargeInitPointFlag);
        if (StringUtils.equals(frFlag,ChargePredictFields.ins().oneStr)){
            //当前line是首条记录
            return;
        }
        //当前line不是首条记录，统一赋值0
        record.put(ChargePredictFields.ins().stdFieldChargeInitPointFlag,ChargePredictFields.ins().zeroStr);
        //需要复制首条记录的状态
        String chargeGroupName = record.getString(ChargePredictFields.ins().dwsFieldChargeBegin);
        String frFieldVal = frState.get(chargeGroupName);
        record.put(ChargePredictFields.ins().stdFieldChargeInitPoint,frFieldVal);
    }
}
